package com.mbbmap.security.action;

//import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mbbmap.app.home.SecAuditTrailHome;
import com.mbbmap.app.home.SecUsersHome;
import com.mbbmap.util.Constants;
public class SecAuditTrailAction extends CommonDispatchAction{

	/**
	 * @param args
	 */
	
	public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String nextpage = "success";
		HttpSession session = request.getSession(false);
		
		System.out.println("Security audit trail action.");
		
		String recordsearch = "";
		String rptdatefr = "";
		String rptdateto = "";
		System.out.println("Get the ");
		recordsearch = sanitize(request.getParameter("recordsearch"));
		rptdatefr = sanitize(request.getParameter("rptdatefr"));
		rptdateto = sanitize(request.getParameter("rptdateto"));
		
		System.out.println("The record is "+recordsearch);
		System.out.println("Date from "+rptdatefr);
		System.out.println("Date to "+rptdateto);
		
		SecAuditTrailHome auditTrail = new SecAuditTrailHome();
		ArrayList<String> auditresult = new ArrayList<String>();
		request.setAttribute("auditaction", "searchAudit");
		
		auditresult = auditTrail.FillDataTable(recordsearch, rptdatefr, rptdateto);
		if (auditresult!=null){
		session.setAttribute("auditresult", auditresult);
		System.out.println("Set the arraylist in session.");
		}
		System.out.println("Set the record search value.");
		session.setAttribute("recordsearch", recordsearch);
		if(!isTokenValid(request)){
			System.out.println("Token validation failed!");
			session.invalidate();
			return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
		}else{
			resetToken(request);
			System.out.println("Token validation passed!");
		}
		return mapping.findForward(nextpage);
		
	}
	
public ActionForward listinUsr(ActionMapping mapping,
	    ActionForm form, HttpServletRequest request,
	    HttpServletResponse response) throws Exception {
		
		System.out.println("Security audit trail action listinactive.java: Start");
		String nextpage = "success";
		HttpSession session = request.getSession(false);
		String strEMerchant = (String) session.getAttribute(Constants.MERCHANT_KEY);
		System.out.println("Security audit trail action listinactive.java: strEMerchant : " + strEMerchant);
		String strMerchant = "";
		if ((strEMerchant != null) && (!strEMerchant.equals(""))){
			//strMerchant= EncryptionHelper.decrypt(strEMerchant,ConfigManager.getInstance().get(EbppKeys.ENCRYPTION_PASSPHRASE));
		    strMerchant= strEMerchant;
		}else{
			strMerchant = "";
			return mapping.findForward("sessionError");
		}
		if(!isTokenValid(request)){
			System.out.println("Token validation failed!");
			session.invalidate();
			return mapping.findForward(Constants.KEY_ACTION_SESSION_ERR);
		}else{
			resetToken(request);
			System.out.println("Token validation passed!");
		}
		
		System.out.println("Security audit trail action listinactive.");
		
		request.setAttribute("auditaction", "listinactive");
		ArrayList<String> Inactiveresult = new ArrayList<String>();		
		Inactiveresult = SecUsersHome.getInstance().getInactiveUsers(strMerchant);
		request.setAttribute("Inactiveresult", Inactiveresult);

		//Inactiveresult
		
		return mapping.findForward(nextpage);
	}

	//public static void main(String[] args) {
		// TODO Auto-generated method stub
	//}

}
